package org.light.simpleauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.complexverb.ListMyActive;
import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;
import org.light.verb.FindByName;

public class ReadMySession extends Verb implements EasyUIPositions {
	protected Domain roleDomain;
	protected Domain privilegeDomain;	

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;		
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}
	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}
	
	public ReadMySession(){
		super();
		this.setLabel("读出会话");
	}
	
	public ReadMySession(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		//this.denied = domain.isVerbDenied("ReadMySession");
		this.setVerbName("ReadMySession");
		this.setLabel("读出会话");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("ReadMySession");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("processRequest");
			method.setReturnType(new Type("void"));
			method.setThrowException(true);
			List<String> list = new ArrayList<String>();
			list.add("ServletException");
			list.add("IOException");
			method.setIsprotected(true);
			method.setOtherExceptions(list);
			method.addSignature(new Signature(1,"request",new Type("HttpServletRequest","javax.servlet.http")));
			method.addSignature(new Signature(2,"response",new Type("HttpServletResponse","javax.servlet.http")));
			method.addAdditionalImport("java.io.IOException");
			method.addAdditionalImport("java.io.PrintWriter");
			method.addAdditionalImport("javax.servlet.ServletException");
			method.addAdditionalImport("javax.servlet.http.HttpServlet");
			method.addAdditionalImport("javax.servlet.http.HttpServletRequest");
			method.addAdditionalImport("javax.servlet.http.HttpServletResponse");
			method.addAdditionalImport("java.util.Map");
			method.addAdditionalImport("java.util.Set");
			method.addAdditionalImport("java.util.HashSet");
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport("java.util.ArrayList");
			method.addAdditionalImport("java.util.TreeMap");
			method.addAdditionalImport("net.sf.json.JSONObject");
			method.addAdditionalImport("javax.servlet.http.HttpSession");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.roleDomain.getDomainSuffix()+"."+this.roleDomain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.privilegeDomain.getDomainSuffix()+"."+this.privilegeDomain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+".utils.StringUtil");
			method.addAdditionalImport(this.domain.getPackageToken()+".utils.AppTokenManager");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceimplSuffix()+"."+this.domain.getStandardName()+"ServiceImpl");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.roleDomain.getServiceSuffix()+"."+this.roleDomain.getStandardName()+"Service");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.roleDomain.getServiceimplSuffix()+"."+this.roleDomain.getStandardName()+"ServiceImpl");

			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,2,"response.setContentType(\"application/json;charset=UTF-8\");"));
			sList.add(new Statement(2000L,2,"Map<String,Object> result = new TreeMap<String,Object>();"));
			sList.add(new Statement(3000L,2,"PrintWriter out = response.getWriter();"));
			sList.add(new Statement(4000L,2,"try {"));
			sList.add(new Statement(5000L,3,"HttpSession session = request.getSession();"));
			sList.add(new Statement(6000L,3,this.domain.getCapFirstDomainNameWithSuffix()+" "+this.domain.getLowerFirstDomainName()+" = ("+this.domain.getCapFirstDomainNameWithSuffix()+")session.getAttribute(\"user\");"));
			sList.add(new Statement(7000L,3,this.domain.getCapFirstDomainName()+"Service service = new "+this.domain.getCapFirstDomainName()+"ServiceImpl();"));
			sList.add(new Statement(8000L,3,"String token= request.getParameter(\"token\");"));
			sList.add(new Statement(9000L,3,"if (!StringUtil.isBlank(token)) {"));
			sList.add(new Statement(10000L,4,"String "+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+" = AppTokenManager.getUserNameViaToken(token);"));
			sList.add(new Statement(11000L,4,"if (!StringUtil.isBlank("+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+")) {"));
			FindByName findUser = new FindByName(this.domain);
			sList.add(new Statement(12000L,5,this.domain.getLowerFirstDomainName()+" = service."+StringUtil.lowerFirst(findUser.getVerbName())+"("+this.domain.findFieldByFixedName("userName").getLowerFirstFieldName()+");"));
			sList.add(new Statement(13000L,4,"}"));
			sList.add(new Statement(14000L,3,"}"));
			sList.add(new Statement(15000L,3,"if("+this.domain.getLowerFirstDomainName()+"!=null) {"));
			sList.add(new Statement(16000L,4,this.roleDomain.getCapFirstDomainName()+"Service "+this.roleDomain.getLowerFirstDomainName()+"Service = new "+this.roleDomain.getCapFirstDomainName()+"ServiceImpl();"));
			sList.add(new Statement(17000L,4,"Set<String> "+this.roleDomain.getLowerFirstDomainName()+"Names=new HashSet<String>();"));
			sList.add(new Statement(18000L,4,"List<String> "+this.privilegeDomain.getLowerFirstDomainName()+"NameList=new ArrayList<String>();"));
			sList.add(new Statement(19000L,4,""));
			ListMyActive listMyRoles = new ListMyActive(this.domain,this.roleDomain);
			sList.add(new Statement(20000L,4,"Set<"+this.roleDomain.getCapFirstDomainNameWithSuffix()+"> "+this.roleDomain.getLowerFirstPlural()+" = service."+org.javaforever.gatescore.utils.StringUtil.lowerFirst(listMyRoles.getVerbName())+"("+this.domain.getLowerFirstDomainName()+".get"+this.domain.getDomainId().getCapFirstFieldName()+"());"));
			sList.add(new Statement(21000L,4,"for("+this.roleDomain.getCapFirstDomainNameWithSuffix()+" "+this.roleDomain.getLowerFirstDomainName()+":"+this.roleDomain.getLowerFirstPlural()+"){"));
			sList.add(new Statement(22000L,6,this.roleDomain.getLowerFirstDomainName()+"Names.add("+this.roleDomain.getLowerFirstDomainName()+".get"+this.roleDomain.getDomainName().getCapFirstFieldName()+"());"));
			ListMyActive listMyPrivs = new ListMyActive(this.roleDomain,this.privilegeDomain);
			sList.add(new Statement(23000L,6,"Set<"+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+"> "+this.privilegeDomain.getLowerFirstPlural()+" = "+this.roleDomain.getLowerFirstDomainName()+"Service."+StringUtil.lowerFirst(listMyPrivs.getVerbName())+"("+this.roleDomain.getLowerFirstDomainName()+".get"+this.roleDomain.getDomainId().getCapFirstFieldName()+"());"));
			sList.add(new Statement(24000L,6,"for ("+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+" "+this.privilegeDomain.getLowerFirstDomainName()+": "+this.privilegeDomain.getLowerFirstPlural()+") {"));
			sList.add(new Statement(25000L,8,this.privilegeDomain.getLowerFirstDomainName()+"NameList.add("+this.privilegeDomain.getLowerFirstDomainName()+".get"+this.privilegeDomain.getDomainName().getCapFirstFieldName()+"());"));
			sList.add(new Statement(26000L,6,"}"));
			sList.add(new Statement(27000L,4,"}"));
			sList.add(new Statement(28000L,2,""));
			sList.add(new Statement(29000L,4,"result.put(\"success\",true);"));
			sList.add(new Statement(30000L,4,"result.put(\"user\", "+this.domain.getLowerFirstDomainName()+");"));
			sList.add(new Statement(31000L,4,"result.put(\"roles\", "+this.roleDomain.getLowerFirstDomainName()+"Names);"));
			sList.add(new Statement(32000L,4,"result.put(\"perms\", "+this.privilegeDomain.getLowerFirstDomainName()+"NameList);"));
			sList.add(new Statement(33000L,3,"} else {"));
			sList.add(new Statement(34000L,4,"result.put(\"success\",false);"));
			sList.add(new Statement(35000L,4,"result.put(\"user\",null);"));
			sList.add(new Statement(36000L,3,"}"));
			sList.add(new Statement(37000L,3,"out.print(JSONObject.fromObject(result).toString());"));
			sList.add(new Statement(38000L,2,"} catch (Exception e){"));
			sList.add(new Statement(39000L,3,"e.printStackTrace();"));
			sList.add(new Statement(40000L,3,"result.put(\"success\",false);"));
			sList.add(new Statement(41000L,3,"result.put(\"reason\",e.getMessage());"));
			sList.add(new Statement(42000L,3,"out.print(JSONObject.fromObject(result).toString());"));
			sList.add(new Statement(43000L,2,"} finally {"));
			sList.add(new Statement(44000L,3,"out.close();"));
			sList.add(new Statement(45000L,2,"}"));

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}	
	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;	
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;		
	}

	public Domain getRoleDomain() {
		return roleDomain;
	}

	public void setRoleDomain(Domain roleDomain) {
		this.roleDomain = roleDomain;
	}

	public Domain getPrivilegeDomain() {
		return privilegeDomain;
	}

	public void setPrivilegeDomain(Domain privilegeDomain) {
		this.privilegeDomain = privilegeDomain;
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyServiceImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyDaoMethodDefinition() throws Exception {
		return null;
	}	
}
